package day20170505.homework.ch02;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.junit.Test;

/**
 * 括号是否配对工具
 * 
 * @author 罗智壕-13424431416
 *
 *         2017年5月5日
 */
public class CheckStack {

	/**
	 * 用Junit进行测试多少个字母
	 * 
	 * @throws Exception
	 */
	@Test
	public void checkEnglishTest() {

		// 创造一个侦察兵
		File file = new File("C:/Users/Administrator/Desktop/Node.java");
		// 创造一个小部队
		Reader reader = null;
		// 创造一个大部队
		BufferedReader br = null;
		try {
			reader = new FileReader(file);
			br = new BufferedReader(reader);
			char[] ch = new char[1];
			int len = -1;
			int sum = 0;
			while ((len = br.read(ch)) != -1) {
				if (ch[0] >= 'a' && ch[0] <= 'z' || ch[0] >= 'A' && ch[0] <= 'Z') {
					sum++;
				}
			}
			System.out.println("总共出现了" + sum + "个字母");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 用Junit进行测试配对工具
	 * 
	 * @throws Exception
	 */
	@Test
	public void checkStackTest() {

		// 创造一个侦察兵
		File file = new File("C:/Users/Administrator/Desktop/Node.java");
		// 创造一个小部队
		Reader reader = null;
		// 创造一个大部队
		BufferedReader br = null;
		try {
			reader = new FileReader(file);
			br = new BufferedReader(reader);
			char[] ch = new char[1];
			int len = -1;
			int pop = 0;
			int to = 0;
			while ((len = br.read(ch)) != -1) {
				if (ch[0] == '{') {
					to++;
				}
				if (ch[0] == '}') {
					pop++;
				}
			}

			int sum = to - pop;
			if (sum == 0) {
				System.out.println("格式正确...");
			} else {
				System.out.println("缺少了" + sum + "个右括号");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 用Junit进行检测代码
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	@Test
	public void checkAllTest() {
		// 创造一个侦察兵
		File file = new File("C:/Users/Administrator/Desktop/Node.java");
		// 创造一个小部队
		Reader reader = null;
		// 创造一个大部队
		BufferedReader br = null;
		try {
			reader = new FileReader(file);
			br = new BufferedReader(reader);
			char[] ch = new char[1];
			int len = -1;
			//统计字母的次数
			int sum = 0;
			//统计注释多少次
			int txtComment = 0;
			//小括号次数
			int kuo = 0;
			//总共多少行代码
			int codeSum = 0;
			while ((len = br.read(ch)) != -1) {
				if (ch[0] >= 'a' && ch[0] <= 'z' || ch[0] >= 'A' && ch[0] <= 'Z') {
					sum++;
				}
				if(ch[0] == '/'){
					txtComment++;
				}
				if(ch[0] == '('){
					kuo++;
				}
				if(ch[0] == ';'){
					codeSum++;
				}
				if(ch[0] == '{'){
					codeSum++;
				}
			}
			System.out.println("总共出现了" + sum + "个字母");
			System.out.println("总共出现了" + txtComment/2 + "个注释");
			System.out.println("总共出现了" + kuo + "个小括号");
			System.out.println("总共出现了" + codeSum + "行代码");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
